﻿using System.ComponentModel;
using System.Reflection;

namespace Token.Utils.DotNetExtension;

public static class AttributeExtension
{
    /// <summary>
    /// 获取类的指定特性
    /// </summary>
    /// <param name="type"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static T? GetAttribute<T>(this Type type) where T : Attribute
    {
        return type.GetCustomAttribute<T>();
    }
    
    /// <summary>
    /// 获取类的 Description 特性
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public static string GetDescription(this Type type)
    {
        var descriptionAttribute = type.GetAttribute<DescriptionAttribute>();
        return descriptionAttribute?.Description ?? string.Empty;
    }
}